package com.dibang.app.mvp.util;

import android.content.Context;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

public class LocationService {

    private static volatile LocationService sInstance = null;

    private static AMapLocationClient mClient;

    private static AMapLocationClientOption mOption;

    private LocationService() {

    }

    /***
     * 初始化
     * @param context
     */
    public void init(Context context) {
        if (mClient == null) {
            mClient = new AMapLocationClient(context.getApplicationContext());
            mClient.setLocationOption(getDefaultLocationClientOption());
        }
    }

    /**
     * 获取单例
     *
     * @return
     */
    public static LocationService get() {
        if (sInstance == null) {
            synchronized (LocationService.class) {
                if (sInstance == null) {
                    sInstance = new LocationService();
                }
            }
        }
        return sInstance;
    }

    /***
     * 注册定位监听
     * @param listener
     * @return
     */

    public LocationService registerListener(AMapLocationListener listener) {
        if (listener != null) {
            mClient.setLocationListener(listener);
        }
        return this;
    }

    /**
     * 注销定位监听
     *
     * @param listener
     */
    public LocationService unregisterListener(AMapLocationListener listener) {
        if (listener != null) {
            mClient.unRegisterLocationListener(listener);
        }
        return this;
    }

    /***
     * 设置定位参数
     * @param option
     * @return
     */
    public boolean setLocationOption(AMapLocationClientOption option) {
        if (option != null) {
            if (mClient.isStarted()) {
                mClient.stopLocation();
            }
            mClient.setLocationOption(option);
            return true;
        }
        return false;
    }

    /***
     *
     * @return DefaultLocationClientOption  默认O设置
     */
    public AMapLocationClientOption getDefaultLocationClientOption() {
        if (mOption == null) {
            mOption = new AMapLocationClientOption();
            // 设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            // 设置是否返回地址信息（默认返回地址信息）
            mOption.setNeedAddress(true);
            // 设置是否只定位一次,默认为false
            mOption.setOnceLocation(false);
            // 设置是否强制刷新WIFI，默认为强制刷新
            mOption.setWifiActiveScan(true);
            // 设置是否允许模拟位置,默认为false，不允许模拟位置
            mOption.setMockEnable(false);
            // 设置定位间隔,单位毫秒,默认为2000ms
            mOption.setInterval(2000);
            // 给定位客户端对象设置定位参数
        }
        return mOption;
    }


    /**
     * @return DIYOption 自定义Option设置
     */
    public AMapLocationClientOption getOption() {
        return mOption;
    }

    /**
     * 开始定位
     *
     * @param listener
     */
    public static void start(AMapLocationListener listener) {
        get().registerListener(listener).start();
    }

    /**
     * 开始定位
     */
    public LocationService start() {
        if (mClient != null && !mClient.isStarted()) {
            mClient.startLocation();
        }
        return this;
    }

    /**
     * 停止定位
     */
    public static void stop(AMapLocationListener listener) {
        get().unregisterListener(listener).stop();
    }

    /**
     * 停止定位
     */
    public LocationService stop() {
        if (mClient != null && mClient.isStarted()) {
            mClient.stopLocation();
        }
        return this;
    }

    public boolean isStart() {
        return mClient.isStarted();
    }

}
